package yt.kpool.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.MainActivity;
import yt.kpool.R;
import yt.kpool.activity.personal.FastLookActivity;
import yt.kpool.activity.personal.LoginActivity;
import yt.kpool.activity.setting.AboutUsActivity;
import yt.kpool.activity.setting.AccountListActivity;
import yt.kpool.activity.setting.ChangeActivity;
import yt.kpool.activity.setting.FastLookListActivity;
import yt.kpool.activity.setting.PaySetAc;
import yt.kpool.activity.setting.PoliceSet;
import yt.kpool.activity.setting.agent.AgentAc;
import yt.kpool.base.BaseApplication;
import yt.kpool.interfac.Observer;
import yt.kpool.interfac.OnSelectPersonListener;
import yt.kpool.util.SPUtil;
import yt.kpool.view.lazyviewpager.LazyFragmentPagerAdapter;

/**
 * 设置
 */
public class SetFragment extends Fragment implements LazyFragmentPagerAdapter.Laziable, Observer {
    @BindView(R.id.img_pay)
    ImageButton img_pay;
    @BindView(R.id.tv_moneyKind)
    TextView tv_moneyKind;

    @BindView(R.id.ly_link)
    LinearLayout ly_link;//全部链接
    @BindView(R.id.ly_safe)
    LinearLayout ly_safe;//修改密码
    @BindView(R.id.ly_account)
    LinearLayout ly_account;//挖币账户
    @BindView(R.id.ly_pay_set)
    LinearLayout ly_pay_set;//付款设置
    @BindView(R.id.ly_mine_center)
    LinearLayout ly_mine_center;//个人中心
    @BindView(R.id.ly_call_set)
    LinearLayout ly_call_set;//报警设置
    @BindView(R.id.ly_login)
    LinearLayout ly_login;//立即登录
    @BindView(R.id.lv_line)
    LinearLayout lv_line;
    @BindView(R.id.ly_agent)
    LinearLayout ly_agent;//代理商

    @BindView(R.id.tv_exit)
    TextView tv_exit;

    private OnSelectPersonListener onSelectPersonListener;// 子账户选择
    private boolean b = true;
    private SPUtil spUtil;

    private static final int Type=3;//type=3 显示接口商

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_set, container, false);
        ButterKnife.bind(this, view);
        if (spUtil == null)
            spUtil = new SPUtil(getContext(), "fileName");
        if (spUtil.getInt(BaseApplication.MONEYKIND, 0) == 2) {
            tv_moneyKind.setText("美元 USD");

        } else if (spUtil.getInt(BaseApplication.MONEYKIND, 0) == 1) {
            tv_moneyKind.setText("人民币 CNY");

        }
        return view;
    }

    /**
     * 点击事件的处理
     *
     * @param view
     */
    @OnClick({R.id.ly_mine_center, R.id.ly_safe, R.id.ly_account, R.id.ly_pay_set, R.id.ly_pay_notice, R.id.ly_call_set, R.id.ly_call_notice, R.id.ly_help, R.id.ly_post, R.id.ly_price, R.id.ly_about_us, R.id.tv_exit, R.id.img_pay, R.id.ly_login, R.id.ly_link,R.id.ly_agent})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ly_agent://代理商入口
                if(spUtil.getInt(BaseApplication.UserType,0)!=Type)
                {
                    Toast.makeText(getActivity(),"您没有访问代理商接口的权限",Toast.LENGTH_SHORT).show();
                    return;
                }
                startActivity(new Intent(getActivity(), AgentAc.class));
                break;
            case R.id.ly_link://全部链接
                startActivityForResult(new Intent(getActivity(), FastLookListActivity.class),21);
                break;
            case R.id.ly_login://立即登录
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                intent.putExtra("type", 1);
                startActivityForResult(intent, 50);
                break;
            case R.id.ly_safe://修改密码
                startActivity(new Intent(getActivity(), ChangeActivity.class));
                break;
            case R.id.ly_account://挖币账户
                startActivityForResult(new Intent(getActivity(), AccountListActivity.class), 20);
                break;
            case R.id.ly_pay_set://付款设置
                getActivity().startActivity(new Intent(getActivity(), PaySetAc.class));
                break;
            case R.id.ly_pay_notice://付款通知

                break;
            case R.id.ly_call_set://报警设置
                getActivity().startActivity(new Intent(getActivity(), PoliceSet.class));
                break;
            case R.id.ly_call_notice://报警通知

                break;
            case R.id.ly_help://帮助中心

                break;
            case R.id.ly_post://提交工单

                break;
            case R.id.ly_price://价格显示
                String kind = "";
                if (spUtil.getInt(BaseApplication.MONEYKIND, 0) == 1) {
                    tv_moneyKind.setText("美元 USD");
                    spUtil.putInt(BaseApplication.MONEYKIND, 2);
                    kind = "2";
                } else if (spUtil.getInt(BaseApplication.MONEYKIND, 0) == 2) {
                    tv_moneyKind.setText("人民币 CNY");
                    spUtil.putInt(BaseApplication.MONEYKIND, 1);
                    kind = "1";
                }

                EventBus.getDefault().post(kind);
                break;
            case R.id.ly_about_us://关于我们
                startActivity(new Intent(getActivity(), AboutUsActivity.class));
                break;
            case R.id.tv_exit://退出

                if (onSelectPersonListener.getIsFastLogin()) {
                    //返回登录账户
                    onSelectPersonListener.selectPersonLayout(5, 0);
                } else {
                    //退出登录
                    onSelectPersonListener.selectPersonLayout(6, 0);
                }
                break;
            case R.id.img_pay://付款通知切换按钮
                if (b) {
                    img_pay.setBackgroundResource(R.mipmap.kai);
                    b = false;
                } else {
                    img_pay.setBackgroundResource(R.mipmap.guan);
                    b = true;
                }
                break;
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setLayout(onSelectPersonListener.getIsFastLogin());

    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (onSelectPersonListener != null)
            setLayout(onSelectPersonListener.getIsFastLogin());

        Log.i("-->", "====设置页====setUserVisibleHint=");
    }

    /**
     * 布局显示
     *
     * @param isfastlogin 是否是快捷登录
     */
    private void setLayout(boolean isfastlogin) {
        if (isfastlogin) {
            ly_link.setVisibility(View.VISIBLE);
            ly_safe.setVisibility(View.GONE);
            ly_account.setVisibility(View.GONE);
            ly_pay_set.setVisibility(View.GONE);
            ly_mine_center.setVisibility(View.GONE);
            ly_login.setVisibility(View.GONE);
            ly_call_set.setVisibility(View.GONE);
            ly_agent.setVisibility(View.GONE);
            lv_line.setVisibility(View.VISIBLE);
            tv_exit.setVisibility(View.VISIBLE);
            tv_exit.setText("返回登录账号");
        } else {
            if ("".equals(spUtil.getString(BaseApplication.TOKEN, ""))) {
                //未登录
                ly_link.setVisibility(View.GONE);
                ly_safe.setVisibility(View.GONE);
                ly_account.setVisibility(View.GONE);
                ly_pay_set.setVisibility(View.GONE);
                ly_call_set.setVisibility(View.GONE);
                ly_mine_center.setVisibility(View.GONE);
                lv_line.setVisibility(View.GONE);
                tv_exit.setVisibility(View.GONE);
                ly_login.setVisibility(View.VISIBLE);
                ly_agent.setVisibility(View.GONE);
            } else {
                //普通登录
                if(spUtil.getInt(BaseApplication.UserType,0)==Type)
                {
                    ly_agent.setVisibility(View.VISIBLE);
                }
                else
                {
                    ly_agent.setVisibility(View.GONE);
                }
                ly_link.setVisibility(View.GONE);
                ly_safe.setVisibility(View.VISIBLE);
                lv_line.setVisibility(View.VISIBLE);
                ly_account.setVisibility(View.VISIBLE);
                ly_pay_set.setVisibility(View.VISIBLE);
                ly_call_set.setVisibility(View.VISIBLE);
                ly_mine_center.setVisibility(View.GONE);
                ly_login.setVisibility(View.GONE);
                ly_login.setVisibility(View.GONE);
                tv_exit.setVisibility(View.VISIBLE);
                tv_exit.setText("退出登录");
            }

        }
    }


    // 当FRagmen被加载到activity的时候会被回调
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (activity instanceof OnSelectPersonListener) {
            onSelectPersonListener = (OnSelectPersonListener) activity; // 2.2 获取到宿主activity并赋值
        } else {
            throw new IllegalArgumentException("activity must implements FragmentInteraction");
        }
    }


    @Override
    public void updatePerson(int type, String name, int id) {

        Log.i("-->", "====设置页=====" + type);
//        setLayout(type == 3);
    }

    @Override
    public void updateCurrency(int img, String name, int id) {

    }

    @Override
    public void fragmentRefresh(int type) {

    }
}
